package lumis.service.sendtofriend;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lumis.portal.PortalException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.controller.ControllerException;
import lumis.portal.controller.ControllerHtml;
import lumis.util.CookieUtil;
import lumis.util.LocaleUtil;

/**
 * Html controller for link of send to friend service. Sets the locale in user current session config and
 * rediret to the link given by url param.
 *
 * @version $Revision: 8289 $ $Date: 2007-09-24 17:46:32 -0300 (Mon, 24 Sep 2007) $
 * @since 4.0.11
 */
public class SendToFriendControllerHtml extends ControllerHtml
{
	public SendToFriendControllerHtml(HttpServletRequest request, HttpServletResponse response) throws ControllerException, PortalException
	{
		super(request, response);
	}

	public void handleLinkRequest() throws PortalException
	{
		try
		{
			String locale = request.getParameter(PortalRequestParameters.PAGE_PARAMETER_LOCALE);
			
			if(locale != null)
			{
				Locale curLocale = LocaleUtil.FromString(locale);
				sessionConfig.setLocale(curLocale);
				CookieUtil.setCookie(request, response, PortalRequestParameters.PAGE_PARAMETER_LOCALE, curLocale.toString());
				SessionConfig.getCurrentSessionConfig().setLocale(curLocale);
			}
			response.sendRedirect(request.getParameter("link"));
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
}
